package com.ffcs.cn.action.base.base;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.utilities.ComponentFactory;
import com.supermap.desktop.core.properties.CoreProperties;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * GirdBagLayout 示例代码
 */
public class DialogGridBagLayout extends SmDialog {
    // 文本框相关组件
    private JLabel labelJTextField;    // 文本框标签
    private JTextField textField;       // 文本输入框

    // 单选按钮相关组件
    private JLabel labelJRadioButton;   // 单选按钮标签
    private JRadioButton radioButton1;  // 第一个单选按钮
    private JRadioButton radioButton2;  // 第二个单选按钮

    // 复选框相关组件
    private JLabel labelJCheckBox;      // 复选框标签
    private JCheckBox checkBox;         // 复选框1（注意变量名与实际显示文本不一致的问题）
    private JCheckBox checkBox1;        // 复选框2
    private JCheckBox checkBox2;        // 复选框3

    // 下拉框相关组件
    private JLabel labelJComboBox;      // 下拉框标签
    private JComboBox<String> comboBox; // 下拉选择框

    // 列表框相关组件
    private JLabel labelJList;          // 列表框标签
    private JList<String> list;          // 列表组件

    // 输出区域相关组件
    private JLabel labelOut;            // 文本域标签
    private JTextArea jTextArea;        // 多行文本显示区域

    // 操作按钮
    private JButton buttonOk;           // 确定按钮
    private JButton buttonCancel;       // 取消按钮

    public DialogGridBagLayout() {
        //设置窗口标题
        setTitle("GridBagLayout布局示例");
        //设置初始化窗口的大小为 460 * 460
        setSize(new Dimension(460, 460));
        //设置窗口的最小为 460 * 460
        setMinimumSize(new Dimension(460, 460));
        //初始化使用的控件
        initComponents();
        //初始化控件布局
        initLayout();
        //添加监听事件
        initListener();
    }

    private void initComponents() {
        //创建文本框
        labelJTextField = new JLabel("文本框");
        textField = new JTextField();

        //创建单选按钮
        labelJRadioButton = new JLabel("单选按钮");
        radioButton1 = new JRadioButton("单选按钮1");
        radioButton2 = new JRadioButton("单选按钮2");
        //创建按钮组，将两个单选按钮添加到该组
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(radioButton1);
        buttonGroup.add(radioButton2);
        //默认选择单选按钮1
        radioButton1.setSelected(true);

        //创建复选按钮
        labelJCheckBox = new JLabel("复选框");
        checkBox = new JCheckBox("复选按钮1");
        checkBox1 = new JCheckBox("复选按钮2");
        checkBox2 = new JCheckBox("复选按钮3");

        //创建下拉框
        labelJComboBox = new JLabel("下拉列表");
        comboBox = new JComboBox<>();
        comboBox.addItem("选项1");
        comboBox.addItem("选项2");
        comboBox.addItem("选项3");
        comboBox.addItem("选项4");

        //创建列表框
        labelJList = new JLabel("列表框");
        list = new JList<>();
        //设置列表数据
        list.setListData(new String[]{"Java", "C++", "Python", "Go"});

        //创建文本区域
        labelOut = new JLabel("文本域");
        jTextArea = new JTextArea(5, 10);

        buttonOk = new JButton(CoreProperties.getString("String_OK"));
        buttonCancel = new JButton(CoreProperties.getString("String_Cancel"));
    }

    private void initLayout() {
        //设置布局为 gridBagLayout 布局方式
        this.setLayout(new GridBagLayout());

        this.add(
                labelJTextField, // 要添加的组件：显示"文本框"文字的标签
                // 创建GridBag约束助手对象，配置组件在网格中的位置和跨度
                new GridBagConstraintsHelper(
                        0, // gridx: 组件位于网格的第0列（最左列）
                        0, // gridy: 组件位于网格的第0行（首行）
                        1, // gridwidth: 组件横向占据1列的宽度
                        1  // gridheight: 组件纵向占据1行的高度
                ).setInsets( // 设置组件外边距（上、左、下、右）
                                GridBagConstraintsHelper.FRAME_CONTROL_GAP, // 上边距：框架与控件的标准间距（通常5-10像素）
                                GridBagConstraintsHelper.FRAME_CONTROL_GAP, // 左边距：与左侧控件的标准间距
                                0,  // 下边距：与下方组件无间距
                                0   // 右边距：与右侧组件无间距
                        )
                        // 设置填充方式：NONE表示不扩展填充空间，保持组件原始尺寸
                        //（若设为HORIZONTAL水平方向扩展/VERTICAL垂直方向扩展/BOTH水平和垂直方向扩展）
                        .setFill(GridBagConstraints.NONE)
                        //NORTHEAST:右上角
                        //CENTER:中心
                        //NORTH:顶部
                        //EAST: 组件放置在显示区域的右侧（东边）。
                        //SOUTH: 组件放置在显示区域的底部（南边）。
                        //SOUTHWEST: 组件放置在显示区域的左下角（西南角）。
                        //SOUTHEAST: 组件放置在显示区域的右下角（东南角）。
                        //NORTHWEST: 组件放置在显示区域的左上角（西北角）。
                        .setAnchor(GridBagConstraints.WEST)
        );


        //添加文本框
        this.add(labelJTextField, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(textField, new GridBagConstraintsHelper(1, 0, 4, 1)
                .setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
                .setWeight(1, 0)
                .setFill(GridBagConstraints.HORIZONTAL));

        //添加单选按钮
        this.add(labelJRadioButton, new GridBagConstraintsHelper(0, 1, 1, 1)
                .setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(radioButton1, new GridBagConstraintsHelper(1, 1, 2, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(radioButton2, new GridBagConstraintsHelper(3, 1, 2, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));

        //添加复选按钮
        this.add(labelJCheckBox, new GridBagConstraintsHelper(0, 2, 1, 1)
                .setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(checkBox, new GridBagConstraintsHelper(1, 2, 1, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE));
        this.add(checkBox1, new GridBagConstraintsHelper(2, 2, 1, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE));
        this.add(checkBox2, new GridBagConstraintsHelper(3, 2, 1, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE));

        //添加下拉框
        this.add(labelJComboBox, new GridBagConstraintsHelper(0, 3, 1, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(comboBox, new GridBagConstraintsHelper(1, 3, 4, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
                .setWeight(1, 0)
                .setFill(GridBagConstraints.HORIZONTAL));

        //添加列表
        this.add(labelJList, new GridBagConstraintsHelper(0, 4, 1, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(list, new GridBagConstraintsHelper(1, 4, 4, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
                .setWeight(1, 0)
                .setFill(GridBagConstraints.HORIZONTAL));

        //将文本域添加到滚动面板中
        JScrollPane scrollPane = new JScrollPane(jTextArea);
        //添加文本域
        this.add(labelOut, new GridBagConstraintsHelper(0, 5, 1, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.WEST));
        this.add(scrollPane, new GridBagConstraintsHelper(1, 5, 4, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.CONTROLS_GAP, 0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
                .setWeight(1, 1)
                .setFill(GridBagConstraints.BOTH));

        //添加确定和取消按钮
        this.add(ComponentFactory.createButtonPanel(buttonOk, buttonCancel), new GridBagConstraintsHelper(0, 6, 5, 1)
                .setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
                .setFill(GridBagConstraints.NONE)
                .setAnchor(GridBagConstraints.EAST));
    }

    private void initListener() {
        ActionListener disposeListener = e -> {
            //关闭 Dialog
            dispose();
        };
        //为确定按钮和取消按钮添加点击事件
        buttonOk.addActionListener(disposeListener);
        buttonCancel.addActionListener(disposeListener);

        ActionListener actionListener = e -> {
            if (e.getSource() instanceof JRadioButton) {
                jTextArea.append(((JRadioButton) e.getSource()).getText() + "\n");
            } else if (e.getSource() instanceof JCheckBox) {
                jTextArea.append(((JCheckBox) e.getSource()).getText() + "\n");
            } else if (e.getSource() instanceof JComboBox) {
                jTextArea.append(((JComboBox) e.getSource()).getSelectedItem() + "\n");
            }
        };
        //为 JRadioButton 添加点击事件
        radioButton1.addActionListener(actionListener);
        radioButton2.addActionListener(actionListener);

        //为 JCheckBox 添加点击事件
        checkBox.addActionListener(actionListener);
        checkBox1.addActionListener(actionListener);
        checkBox2.addActionListener(actionListener);

        //为 JComboBox 添加点击事件
        comboBox.addActionListener(actionListener);

        //为 JList 添加点击事件
        list.addListSelectionListener(e -> {
            if (e.getValueIsAdjusting()) {
                return;
            }
            jTextArea.append(list.getSelectedValuesList() + "\n");
        });
    }
}
